<?php
namespace Home\Controller;
use Think\Controller;
// +----------------------------------------------------------------------
// | Date:2017年9月18日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class ProductController extends  BaseController{
    protected $cookie_key = 'detail_visit';
    
    /**
     * 根据二级目录tid， 获取商品信息
     *
     * date:2017年9月15日
     * author: EK_熊
     */
    public function index(){
        $tid = I('tid');
        $tsid = I('tsid');
        $page = I('p',1);
        $limit = I('limit',9);
        $cateChild = $this->getchild($tid);
        
        if (!$tsid) {
            $childid[] = $tid;
            foreach ($cateChild as $k=>$v){
                $childid[] = $v['id'];
            }
        }else{
            $childid[] = $tsid;
        }
        $list = D('Posts')->getList($childid,$page,$limit);
        
        $map['term_id'] = ['in',$childid];
        $map['status'] = 1;
        $count      = M('TermRelationships')->where($map)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('prev', '&laquo;');
        $Page->setConfig('next', '&raquo;');
        $Page->setConfig('theme','<ul><li>%UP_PAGE%</li> <li>%LINK_PAGE%</li>  <li>%DOWN_PAGE%</li> <li>%HEADER%</li></ul>');

        $show       = $Page->show();// 分页显示输出
        
        
        $navsec = array_values($cateChild);
        $index = [
            'name'=>'全部',
            'id'=>''
        ];
        array_unshift($navsec,$index);
        $crumbs_cur = $tsid ? $tsid : $tid;
        $this->assign('navsec',$navsec);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('list',$list);
        $this->assign('banner',banner($tid));
        $this->assign('crumbs',$this->crumbs($crumbs_cur));
        $this->assign('crumbs_cur',$crumbs_cur);
        $this->display();
    
    }
    
    public function getchild($tid){
        $map = [
            'status'=>1,
        ];
        $upload_path = C("TMPL_PARSE_STRING.__UPLOAD__");
        $result = M('terms')->where($map)->field("term_id as id,name,parent as parentid")->order('listorder')->select();
        $tree = new \Tree();
        $tree->init($result);
        $child = $tree->get_child($tid);
        return $child;
    }
    
    

    
    /**
     * 列表页 面包屑
     * @param unknown $tid
     * @return mixed|NULL|unknown|unknown[]|object
     * date:2017年10月7日
     * author: EK_熊
     */
    public function crumbs($tid){
        $map = [
            'status'=>1,
            'term_id'=>$tid,
        ];
        $termModel = M('terms');
        $result = $termModel->where($map)->getField("path");
        $path_ary = explode('-',$result);
        array_shift($path_ary);
        $http = U('index');
        $catname = $termModel->where(['term_id'=>['in',$path_ary]])->getField("term_id,name,concat('$http/tid/',term_id) as url",true);
        return $catname;
    }
    
    
    
    /**
     * 获取详情页信息
     * 
     * date:2017年9月18日
     * author: EK_熊
     */
    public function detail(){
        $proid = I('proid');
        if (!$proid) $this->ajaxReturn(['msg'=>'请输入正确的参数']);
        
        $map = [
            'post_status'=>1,
            'id'=>$proid,
        ];
        $data = [];
        $field = 'id,post_price,post_sn,post_size,post_weight,post_content,post_title,post_excerpt,post_modified,term_label,smeta,video';
        $detail = M('Posts')->where($map)->field($field)->find();
        $photo = json_decode($detail['smeta'],true);//预览图片
        if ($detail['video']) {
            $videodata = explode(',',$detail['video']);
            $videobox = D('Common/File')->getFile($videodata,'');
            foreach ($videobox as $k=>$v) {
                $video[$k] = __ROOT__.$v['save_path'].$v['save_name'];
            }
        }
        $detail['video'] =$video;
        unset($photo['thumb']);
        unset($detail['smeta']);
        foreach ($photo['photo'] as $k=>$v){
            $detail['photo'][$k] = sp_get_asset_upload_path($v['url']);
        }
        $detail['post_price'] = json_decode($detail['post_price'],true);
        //获取访问路径
        $tid = M('TermRelationships')->where(['object_id'=>$proid])->getField('term_id');
        $data['visit_path'] = $this->crumbs($tid);
        $data['visit_path'][] = ['name'=>$detail['post_title']];
        $data['detail'] = $detail;
        //获取（添加）最近访问列表
        $data['last_visit'] = $this->getLastVisit($proid,6);
        
        //相关商品
        $data['related'] = D('Posts')->getRelatedList(current($data['visit_path'])['term_id'],$proid,3);
        $this->assign('data',$data);
        $this->display();        
    }
    
    /**
     * 设置浏览记录
     * @param number $proid
     * date:2017年9月18日
     * author: EK_熊
     */
    public function getLastVisit($proid=1,$num=6){
        
        $cookie = cookie($this->cookie_key);
        if (!$cookie){
            $cookie = [];
        }
        array_unshift($cookie,$proid);
        $cookie = array_unique($cookie,true);
        if (count($cookie) > $num) array_pop($cookie);
        cookie($this->cookie_key,$cookie);//设置添加cookie
        
        if (count($cookie)>1){
            $list = D('Posts')->getVisitList($cookie,$num);
            return $list;
        }
        
    }
    
    
    
    
    /**
     * 获取详情页的访问路径
     * @param unknown $proid
     * @param unknown $proname
     * date:2017年9月18日
     * author: EK_熊
     */
    private function visit_path($proid,$proname){
        $join = [
            '__TERM_RELATIONSHIPS__ tr ON tr.object_id = post.id',
            '__TERMS__ ts ON ts.term_id = tr.term_id'
        ];
        
        $term = M('Posts')->alias('post')->join($join)->where(['post.id'=>$proid])->field('tr.term_id,parent,name')->find();
        $term_top = M('Terms')->where(['term_id'=>$term['parent']])->field('term_id,name')->find();
        $list = [
            ['tag'=>$term_top['name'],'id'=>$term_top['term_id']],
            ['tag'=>$term['name'],'id'=>$term['term_id']],
            ['tag'=>$proname,'id'=>$proid],
        ];
        return $list;
        
    }
    
    
    
}